import tkinter as tk
from tkinter import messagebox


class TodoApp:
    def __init__(self, master):
        self.master = master
        self.master.title("Todo List App")

        # 创建菜单栏
        menubar = tk.Menu(self.master)

        file_menu = tk.Menu(menubar, tearoff=0)
        file_menu.add_command(label="退出", command=self.master.quit)

        help_menu = tk.Menu(menubar, tearoff=0)
        help_menu.add_command(label="关于", command=self.show_about_dialog)

        reminder_menu = tk.Menu(menubar, tearoff=0)
        reminder_menu.add_command(label="设置提醒", command=self.setup_reminder)

        menubar.add_cascade(label="文件", menu=file_menu)
        menubar.add_cascade(label="帮助", menu=help_menu)
        menubar.add_cascade(label="提醒", menu=reminder_menu)

        self.master.config(menu=menubar)

        # 创建待办事项列表
        self.todo_listbox = tk.Listbox(self.master, width=50)
        self.todo_listbox.pack(padx=10, pady=10)

        # 添加事项按钮
        add_button = tk.Button(self.master, text="添加事项", command=self.open_edit_window)
        add_button.pack(pady=10)

    def open_edit_window(self):
        edit_window = tk.Toplevel(self.master)
        EditTodoItem(edit_window, self)

    def show_about_dialog(self):
        messagebox.showinfo("关于", "这是一个简单的待办事项应用程序")

    def setup_reminder(self):
        messagebox.showinfo("设置提醒", "提醒功能尚未实现")

    def add_todo_item(self, item):
        self.todo_listbox.insert(tk.END, item)

    def edit_todo_item(self, index, item):
        self.todo_listbox.delete(index)
        self.todo_listbox.insert(index, item)

    def delete_todo_item(self, index):
        self.todo_listbox.delete(index)


class EditTodoItem:
    def __init__(self, master, app):
        self.master = master
        self.master.title("编辑待办事项")
        self.app = app

        self.item_name_var = tk.StringVar()
        self.start_time_var = tk.StringVar()
        self.deadline_var = tk.StringVar()
        self.reminder_var = tk.BooleanVar()

        tk.Label(master, text="事项名称:").grid(row=0, column=0, padx=10, pady=10)
        tk.Entry(master, textvariable=self.item_name_var).grid(row=0, column=1, padx=10, pady=10)

        tk.Label(master, text="开始时间:").grid(row=1, column=0, padx=10, pady=10)
        tk.Entry(master, textvariable=self.start_time_var).grid(row=1, column=1, padx=10, pady=10)

        tk.Label(master, text="截止时间:").grid(row=2, column=0, padx=10, pady=10)
        tk.Entry(master, textvariable=self.deadline_var).grid(row=2, column=1, padx=10, pady=10)

        tk.Checkbutton(master, text="提醒", variable=self.reminder_var).grid(row=3, columnspan=2, padx=10, pady=10)
        tk.Entry(master, textvariable=self.deadline_var).grid(row=2, column=1, padx=10, pady=10)

        save_button = tk.Button(master, text="保存", command=self.save_item)
        save_button.grid(row=4, column=0, padx=10, pady=10)

        delete_button = tk.Button(master, text="删除", command=self.delete_item)
        delete_button.grid(row=4, column=1, padx=10, pady=10)

    def save_item(self):
        item_name = self.item_name_var.get()
        start_time = self.start_time_var.get()
        deadline = self.deadline_var.get()
        reminder = self.reminder_var.get()

        if item_name and deadline:
            self.app.add_todo_item(f"{item_name} - 截止时间: {deadline}")
            self.master.destroy()
        else:
            messagebox.showwarning("警告", "事项名称和截止时间不能为空！")

    def delete_item(self):
        self.master.destroy()


# 主程序入口
if __name__ == "__main__":
    root = tk.Tk()
    app = TodoApp(root)
    root.mainloop()